<?php

/**
 * 
 * @author yuxun
 *
 */
class CrossServerCommand extends CConsoleCommand {

	//  */1 * * * * /usr/local/bin/php /data/web/webdata/oss/crontab.php crossServer >> /home/webuser/tmp/crossServer.log
	/**
	 * (non-PHPdoc)
	 * @see CConsoleCommand::run()
	 * 自动将常规跨服转化为匹配跨服
     * 定时任务，先查询重置时间，如果当前时间是重置时间的前3天，则自动转化
	 */
    public function run($args) {
        set_time_limit (0);
    	echo "............................... \n";
    	echo "跨服自动配置定时任务开始 \n";
    	echo "开始时间：".date('Y-m-d H:i:s',time())."\n";
    	
    	//查询重置时间
    	$game = 'mango';
    	$crossList = self::searchInfo('cross');
    	$reset_time = $crossList['reset_time'];
    	$reset_time = strtotime ("-3 day", strtotime($reset_time));
    	 
    	$nowTime = strtotime(date('Y-m-d H:i:00',time()));  //当前时间
    	if($reset_time == $nowTime){
    		//获取符合规定的服务器加入匹配行列中
    		//常规跨服的服务器（结束时间在当前时间之前的可以加入匹配行列中（要踢出未开服的服））
    		//匹配跨服的服务器（所有加进来）
    		$modRoutineCrossCerver = new ModRoutineCrossCerver();
    		$routineList = $modRoutineCrossCerver->getAddMatchServer($nowTime,$game);
    		 
    		$server_ids = '';
    		$fRoutineList = array(); //符合进入匹配行列的常规跨服数据
    		$cerver_ids = '';  //符合进入匹配行列的常规跨服数据主键id，要用来删除关系
    		foreach ($routineList as $key=>$val){
    			$cerver_ids .= $val['id'] . ', ';
    			$server_ids .= $val['server_id'] . ', ';
    			$fRoutineList[$val['server_id']] = $val;
    		}
    		$cerver_ids = substr($cerver_ids,0,strlen($cerver_ids)-2);
    		
    		$server_ids = substr($server_ids,0,strlen($server_ids)-2);
    		$modServerChannel = new ModServerChannel();
    		if($server_ids != ''){
    			$serverChannelList = $modServerChannel->selectChannel($server_ids, $game);
    			foreach ($serverChannelList as $key=>$val){  //如果是备选服这unset(tab=0)
    				if($val['tab'] == 0){
    					unset($fRoutineList[$val['server_id']]);
    				}
    			}
    		}
    		
    		$modMatchCrossSer = new ModMatchCrossSer();
    		$modMatchCrossServer = new ModMatchCrossServer();
    		$fmatchList = $modMatchCrossServer->getAddMatchServer($game); //符合进入匹配行列匹配跨服数据
    		
    		//查询在常规跨服的服务器（结束时间在当前时间之后的可以加入匹配行列中（要踢出未开服的服））
    		$delRoutineList = $modRoutineCrossCerver->getdelMatchServer($nowTime,$game);
    		$delRoutineList_array = array();
    		foreach ($delRoutineList as $key=>$val){
    			$delRoutineList_array[] = $val['server_id'];
    		}
    		
    		//还需要剔除已经使用的匹配跨服数据中存在常规跨服的数据
    		foreach($fmatchList as $key=>$val){
    			if(in_array($val['server_id'], $delRoutineList_array)){
    				unset($fmatchList[$key]);
    			}
    		}
    		
    		$fserver_ids = '';  //最终符合规定的服务器ids
    		$routine_cross_ss_ids = ''; //常规跨服的服数据加入之后匹配跨服之后需要删除的关系
    		foreach ($fRoutineList as $key=>$val){
    			$fserver_ids .= $val['server_id'] . ', ';
    			$routine_cross_ss_ids .= $val['id'] . ', ';
    		}
    		foreach ($fmatchList as $key=>$val){
    			$fserver_ids .= $val['server_id'] . ', ';
    		}
    		$fserver_ids = substr($fserver_ids,0,strlen($fserver_ids)-2);
    		$routine_cross_ss_ids = substr($routine_cross_ss_ids,0,strlen($routine_cross_ss_ids)-2);
    		
    		if($fserver_ids == ''){
    			echo "不存在符合规定的匹配数据 \n";
    			exit;
    		}
    		
    		//查询服务器的信息，目前先将按各服的开服时间作为排序
    		$modServer = new ModServer();
    		$fserverList = $modServer->getOrderServerForIds($game, $fserver_ids);
    		
    		$match_automatic_num = $crossList['match_automatic_num'];  //每一组的匹配跨服最大服务器个数
    		$match_cross = array();
    		$d = array();
    		$l = count($fserverList);
    		//根据配置的匹配跨服自动数量限制进行分组
    		foreach ($fserverList as $key=>$val){
    			$n = $key + 1;
    			if($n % $match_automatic_num == 0){
    				$d[] = $val['id'];
    				$match_cross[] = $d;
    				$d = array();
    			}else{
    				$d[] = $val['id'];
    				if($n == $l){
    					$match_cross[] = $d;
    				}
    			}
    		}
    		
    		//将分组配置到各个匹配跨服，再保存到临时的匹配跨服表和关系表
    		//临时的匹配跨服表和关系表每次生效之后都要清空，查询到若有数据，则先删除
    		$modMatchCrossServer_v = new ModMatchCrossServerV();
    		$v_obj = $modMatchCrossServer_v->findAllByAttributes(array('game'=>$game));
    		if($v_obj){
    			//存在数据则删除
    			foreach ($v_obj as $key=>$val){
    				$modMatchCrossServerV = new ModMatchCrossServerV();
    				$modMatchCrossServerV->deleteByPk($val['id']);
    		
    				//删除常规跨服服务器与服务器的关系
    				$modMatchCrossSerV = new ModMatchCrossSerV();
    				$modMatchCrossSerV->deleteAll('match_id=:match_id', array(':match_id'=>$val['id']));
    			}
    		}
    		//获取匹配跨服的配置 和 常规跨服 的配置
    		$modMatchCrossServer = new ModMatchCrossServer();
    		$setMatchList = $modMatchCrossServer->findAllByAttributes(array('game'=>$game,'is_delete'=>0));
    		$set_array = array();
    		foreach ($setMatchList as $key=>$val){
    			$sd = array();
    			$sd['node_name'] = $val->node_name;
    			$sd['ip'] = $val->ip;
    			$sd['port'] = $val->port;
    			$sd['version'] = $val->version;
    			$sd['develop_id'] = $val->develop_id;
    			array_push($set_array, $sd);
    		}
    		
    		$modRoutineCrossCerver = new ModRoutineCrossCerver();
    		$setRoutineList = $modRoutineCrossCerver->findAllByAttributes(array('game'=>$game));
    		foreach ($setRoutineList as $key=>$val){
    			$sd = array();
    			$sd['node_name'] = $val->node_name;
    			$sd['ip'] = $val->ip;
    			$sd['port'] = $val->port;
    			$sd['version'] = $val->version;
    			$sd['develop_id'] = $val->develop_id;
    			array_push($set_array, $sd);
    		}
    		
    		
    		$modMatchCrossSer_v = new ModMatchCrossSerV();
    		$march_server_v = array();
    		foreach ($match_cross as $key=>$val){
    			if(isset($set_array[$key])){  //存在
    				$node_name = $set_array[$key]['node_name'];
    				$ip = $set_array[$key]['ip'];
    				$port = $set_array[$key]['port'];
    				$version = $set_array[$key]['version'];
    				$develop_id = $set_array[$key]['develop_id'];
    			}else{
    				$node_name = '';
    				$ip ='';
    				$port = '';
    				$version = '';
    				$develop_id = '';
    			}
    		
    			$modMatchCrossServer_v = new ModMatchCrossServerV();
    			$modMatchCrossServer_v->game = $game;
    			$modMatchCrossServer_v->cross_server_name = '匹配跨服'.$key;
    			$modMatchCrossServer_v->node_name = $node_name;
    			$modMatchCrossServer_v->ip = $ip;
    			$modMatchCrossServer_v->port = $port;
    			$modMatchCrossServer_v->version = $version;
    			$modMatchCrossServer_v->develop_id = $develop_id;
    			$modMatchCrossServer_v->create_user   = 1;
    			$modMatchCrossServer_v->create_time   = time();
    			$modMatchCrossServer_v->update_user   = 1;
    			$modMatchCrossServer_v->update_time   = time();
    			$modMatchCrossServer_v->save();
    			$match_id = $modMatchCrossServer_v->id;
    			foreach ($val as $k=>$v){
    				$d = array();
    				$d['server_id'] = $v;
    				$d['match_id'] = $match_id;
    				array_push($march_server_v, $d);
    			}
    		}
    		$modMatchCrossSer_v->addmuch($march_server_v);
    		 
    		//抽空那些已经从常规跨服到匹配跨服的数据
    		$modRoutineCrossSer = new ModRoutineCrossSer();
    	    if($cerver_ids != ''){
    			$modRoutineCrossSer->deleteForIds($cerver_ids);
    		} 
    		echo "匹配跨服配置自动生成成功 \n";
    	}else{
    		echo "暂未到重置时间的前3天 \n";
    	}
    	
    	echo "结束时间：".date('Y-m-d H:i:s',time())."\n";
    	echo "跨服自动配置定时任务结束 \n";
    }
    
    
    public static function searchInfo($type){
    	$modSystem = new ModSystem();
    	$game = 'mango';
    	$params = array();
    	$params['g'] = $game;
    	$params['inc_type'] = $type;
    	$list = $modSystem->select($params);
    	$data = array();
    	foreach ($list as $key=>$val){
    		$data[$val['name']] = $val['value'];
    	}
    	return $data;
    }

   

        
}